package ${sourceBasePackage}.service.impl<#if sourceIncludeModule == 'Y'>.${moduleName}</#if>;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.wn.common.core.domain.PageSupport;
import ${sourceBasePackage}.dto<#if sourceIncludeModule == 'Y'>.${moduleName}</#if>.${entityName}Dto;
import ${sourceBasePackage}.dto<#if sourceIncludeModule == 'Y'>.${moduleName}</#if>.${entityName}SearchDto;
import ${sourceBasePackage}.entity<#if sourceIncludeModule == 'Y'>.${moduleName}</#if>.${entityName};
import ${sourceBasePackage}.mapper<#if sourceIncludeModule == 'Y'>.${moduleName}</#if>.${entityName}Mapper;
import ${sourceBasePackage}.service<#if sourceIncludeModule == 'Y'>.${moduleName}</#if>.${entityName}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * ${description}Service实现类
 * @author ${author}
 */
@Service("${entityName?uncap_first}Service")
public class ${entityName}ServiceImpl implements ${entityName}Service {

	@Autowired
	private ${entityName}Mapper ${entityName?uncap_first}Mapper;

	@Override
	public PageInfo<${entityName}Dto> getListByPage(${entityName}SearchDto ${entityName?uncap_first}SearchDto) {
		<#list searchFields as po>
			<#if po.listQuery == 'interval' && po.propertyType == 'Date'>
		if(ObjectUtil.isNotEmpty(${entityName?uncap_first}SearchDto.get${po.propertyName?cap_first}End())) {
			${entityName?uncap_first}SearchDto.set${po.propertyName?cap_first}End(DateUtil.endOfDay(${entityName?uncap_first}SearchDto.get${po.propertyName?cap_first}End()));
		}
			</#if>
		</#list>
		PageSupport.startPage();
		List<${entityName}Dto> list = ${entityName?uncap_first}Mapper.getList(${entityName?uncap_first}SearchDto);
		return new PageInfo<>(list);
	}

	@Override
	public List<${entityName}Dto> getList(${entityName}SearchDto ${entityName?uncap_first}SearchDto) {
		<#list searchFields as po>
			<#if po.listQuery == 'interval' && po.propertyType == 'Date'>
		if(ObjectUtil.isNotEmpty(${entityName?uncap_first}SearchDto.get${po.propertyName?cap_first}End())) {
			${entityName?uncap_first}SearchDto.set${po.propertyName?cap_first}End(DateUtil.endOfDay(${entityName?uncap_first}SearchDto.get${po.propertyName?cap_first}End()));
		}
			</#if>
		</#list>
		List<${entityName}Dto> list = ${entityName?uncap_first}Mapper.getList(${entityName?uncap_first}SearchDto);
		return list;
	}

	@Override
	public ${entityName}Dto getById(${pkPropertyType} ${pkPropertyName}) {
		${entityName}Dto ${entityName?uncap_first}Dto = ${entityName?uncap_first}Mapper.getById(${pkPropertyName});
		if(${entityName?uncap_first}Dto == null) {
			throw new RuntimeException("数据不存在");
		}
		return ${entityName?uncap_first}Dto;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void add(${entityName}Dto ${entityName?uncap_first}Dto) {
		${entityName} ${entityName?uncap_first} = BeanUtil.toBean(${entityName?uncap_first}Dto, ${entityName}.class);
		${entityName?uncap_first}Mapper.insert(${entityName?uncap_first});
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void update(${entityName}Dto ${entityName?uncap_first}Dto) {
		${entityName} ${entityName?uncap_first} = ${entityName?uncap_first}Mapper.selectById(${entityName?uncap_first}Dto.get${pkPropertyName?cap_first}());
		if(${entityName?uncap_first} == null) {
			throw new RuntimeException("数据不存在");
		}
		BeanUtil.copyProperties(${entityName?uncap_first}Dto, ${entityName?uncap_first}, CopyOptions.create().setIgnoreNullValue(true));
		${entityName?uncap_first}Mapper.updateById(${entityName?uncap_first});
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delete(${pkPropertyType} ${pkPropertyName}) {
		${entityName} ${entityName?uncap_first} = ${entityName?uncap_first}Mapper.selectById(${pkPropertyName});
        if(${entityName?uncap_first} == null) {
			throw new RuntimeException("数据不存在");
        }
		${entityName?uncap_first}Mapper.deleteById(${pkPropertyName});
	}

}
